package com.platform.netty.tcp.initializer;

import com.platform.common.utils.netty.NettyDecoder;
import com.platform.netty.tcp.handler.TcpServerHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author: [青衫] 'QSSSYH@QQ.com'
 * @Date: 2019-07-08 9:59
 * @Description: < 描述 >
 */
@Component
public class TcpServerInitializer extends ChannelInitializer<SocketChannel> {

	@Autowired
	TcpServerHandler tcpServerHandler;

	@Override
	protected void initChannel(SocketChannel socketChannel) throws Exception {
		ChannelPipeline pipeline = socketChannel.pipeline();
		//IdleStateHandler心跳机制,如果超时触发Handle中userEventTrigger()方法
		pipeline.addLast("idleStateHandler",
				new IdleStateHandler(15, 0, 0, TimeUnit.MINUTES));
		pipeline.addLast(new NettyDecoder());
		//自定义Handler
		pipeline.addLast("tcpServerHandler", tcpServerHandler);
	}
}